<?php
namespace Home\Controller;

use Library\UploadFile;
use Think\Controller;
use Think\Image;

class IndexController extends Controller {

    private $model;

    public function __construct()
    {
        parent::__construct();

        $this->model = M('photo');

    }
    //首页
    public function index(){

        $this->show();
    }

    function getExif($img){

        $exif = exif_read_data($img, 'IFD0');

        return array (
            '文件名' => $exif['FileName'],
            '器材品牌' => $exif['Make'],
            '器材' => $exif['Model'],
            '快门' => $exif['ExposureTime'],
            '光圈' => $exif['FNumber'],
            '焦距' => $exif['FocalLength'],
            '感光度' => $exif['ISOSpeedRatings']
        );

    }

    //制作海报
    public function makeDo(){
        if(empty($_FILES['file'])){
            $this->error('请上传你的靓照');
        }
        if(!I('post.name')){
            $this->error('请填写你的姓名');
        }

        //上传图片
        $up = new UploadFile();
        $info = $up->upFile('cover', array('jpg', 'gif', 'png', 'jpeg', 'JPEG', 'JPG', 'PNG', 'GIF', 'bmp', 'BMP'));
        $up_path = C('UPLOADS')['PATH'] . $info['file']['savepath'];
        $up_name = $info['file']['savename'];
        $up_file = $up_path . $up_name;

        //处理图片
        $image = new Image();
        $image -> open($up_file);

        $width = $image->width();
        $height = $image->height();

        if($width > 1280){
            $image->thumb(1280,(1280 * $height) / $width,$image::IMAGE_THUMB_CENTER)
                ->save($up_path . $up_name);

            $image->open($up_path . $up_name);
        }

        //获取图片角度
//        $direction =I('direction');
//
//        switch($direction) {
//            case 6://需要顺时针（向左）90度旋转
//                $angle = 270;
//                break;
//            case 8://需要逆时针（向右）90度旋转
//                $angle = 90;
//                break;
//            case 3://需要180度旋转
//                $angle = 180;
//                break;
//            default:
//                $angle = 0;
//                break;
//        }

        //旋转
        $image->rotate(0);

        if($width > $height){
            //旋转
            $image->rotate(270);
        }

        $image->save($up_path . $up_name);

        //如果是iPhone手机相机拍摄旋转90度
//        $deviceType = $this->get_device_type();
//
//        if($deviceType == "ios"){
//
//            //旋转
//            $image->rotate(270);
//
//            $image->save($up_file);
//        }

        //判断图片是否旋转
//        $image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
//        $exif = exif_read_data($_FILES['image_upload']['tmp_name']);

//        $path=$_SERVER['DOCUMENT_ROOT'].$up_file;
//
//        echo $path;
//
//        $exif = exif_read_data($up_file, 'IFD0');
//
//        if(!empty($exif['Orientation'])) {
//            switch($exif['Orientation']) {
//                case 8:
//                    $image = imagerotate($image,90,0);
//                    break;
//                case 3:
//                    $image = imagerotate($image,180,0);
//                    break;
//                case 6:
//                    $image = imagerotate($image,-90,0);
//                    break;
//            }
//
//            //保存图片
//            $image->save($up_file);
//
//        }

        //添加水印
//        $water = './Public/Static/Img/dianxin_water.png';
//
//        $image->save($up_path . $up_name);
//
//        $image->open($up_path.$up_name)
//        ->thumb(720,1280,Image::IMAGE_THUMB_CENTER)
//        ->water($water,Image::IMAGE_WATER_CENTER,100)
//        ->save($up_path . 'w_' . $up_name);

        //            'w_cover' => $info['file']['savepath'] .'w_'. $info['file']['savename'],

        //添加海报数据
        $act = $this->model->add(array(
            'cover' => $info['file']['savepath'] . $info['file']['savename'],
            'create_time' => time(),
            'last_time' => time(),
            'name' => I('post.name')
        ));

        //制作成功后显示海报图片
        if($act){

            //获取id
            $data = $this->model->order('id desc')->limit(1)->find();
            $id=$data['id'];

            //$this->success('制作成功',U('showPoster',array('id' => $id)));
//            $this->redirect(U('showPoster',array('id' => $id)));
//            $this->redirect('showPoster', array('id' => $id), 1, '制作成功');

            //预览
            $this->redirect('preview', array('id' => $id), 1, '跳转预览...');

//            $url=U('preview',array('id'=>$id));


           // $this->success('制作成功',U('showPoster'));
        }
    }

    //保存预览图片
    public function savePreviewImg(){
        $data=$this->model->create();

        //获取图片
        $up_file = C('UPLOADS')['PATH'] .$data['cover'];

        //获取处理图片的属性
        $x = $_POST['x'];          //x坐标
        $y = $_POST['y'];          //y坐标
        $w = $_POST['w'];          //宽度
        $h = $_POST['h'];          //高度

        $scale = $_POST['scale'];  //缩放比例
        $angle = $_POST['angle'];  //旋转角度

        //处理图片
        $image = new Image();
        $image -> open($up_file);

        //先进行缩放处理
//        if($scale){
            $width = $image->width(); // 返回图片的宽度
            $height = $image->height(); // 返回图片的高度

            //按比例缩放图片
            $image->thumb($w, $height)->save($up_file);

//            echo '缩放 '.$w;
//
//           return;

//        }

        //旋转图片
        if($angle){
            $image->rotate(-$angle)->save($up_file);

//            echo '旋转'.$angle;
        }else{

            //裁剪图片
            if($x || $y){

                $image->crop($w,$h,$x,$y)->save($up_file);
//
//                echo '裁剪'.$x.'___  '.$y;
            }

        }

        //保存图片
        $result=$image->save($up_file);

        //保存成功
        if($result){
            //跳转显示海报
            //获取id
            $id=$data['id'];

            $this->success('制作成功',U('showPoster',array('id' => $id)));

        }
        else{
            //失败返回

            $this->error('保存失败');
        }
    }

    //显示海报
    public function showPoster($id){

        $picData = M('photo')->where('id='.$id)->find();

        //$picData = $this->model->order('id desc')->limit(1)->find();
        if(empty($picData)){

            $this->error('找不到数据');
        }else{
            $this->assign('data',$picData);

            $this->display('show');
        }
    }

    //预览海报
    public function preview($id){

        $picData = M('photo')->where('id='.$id)->find();

//        $up_file =  C('UPLOADS')['PATH'] . $picData['cover'];
//        //处理图片
//        $image = new Image();
//        $image -> open($up_file);
//        //旋转
//        $image->rotate(0);
//        $image->save($up_file);

        $this->assign('data',$picData);

        $this->show();
    }

    //裁剪图片
    public function scope(){

        $this->show();
    }

    //判断系统
    function get_device_type()
    {
        //全部变成小写字母
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $type = 'other';
        //分别进行判断
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
            $type = 'ios';
        }

        if(strpos($agent, 'android'))
        {
            $type = 'android';
        }
        return $type;
    }

    //获取设备信息
    function getDeviceInfo(){

        //获取USER AGENT
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);

        //分析数据
        $is_pc = (strpos($agent, 'windows nt')) ? true : false;
        $is_iphone = (strpos($agent, 'iphone')) ? true : false;
        $is_ipad = (strpos($agent, 'ipad')) ? true : false;
        $is_android = (strpos($agent, 'android')) ? true : false;

        //输出数据
        if($is_pc){
            echo "这是PC";
        }
        if($is_iphone){
            echo "这是iPhone";
        }
        if($is_ipad){
            echo "这是iPad";
        }
        if($is_android){
            echo "这是Android";
        }

    }

    public function demo(){

        $this->show();

    }
}